using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services.Protocols;
using System.Net;

using System.Xml;
using System.Collections;
using System.Collections.Specialized;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            /*GlobalWeather.GlobalWeather gw = new GlobalWeather.GlobalWeather();
            String response = gw.GetCitiesByCountry("INDIA");
            tCities.Text = response;*/

            Util.userDetails = new UserDetails();
            getUserDetails();
            Label2.Text = "Welcome "+Util.userDetails.description;

            showAvailableOrganizations();
        }
    }

    private void showAvailableOrganizations()
    {
        ArrayList orglist = Util.userDetails.organizationsList;
        foreach (Organization org in orglist)
        {
            RadioButtonList1.Items.Add(new ListItem(org.description, org.dn));
            if (org.isdefault)
            {
                RadioButtonList1.SelectedValue = org.dn;
            }
        }

    }


    private void getUserDetails()
    {
       
        /*cin400332l_80.GetUserDetails gud = new cin400332l_80.GetUserDetails();
        cin400332l_80.GetUserDetailsBinding gudbinding = new cin400332l_80.GetUserDetailsBinding();
        gudbinding.CookieContainer = Util.getCookieContainer();
        XmlElement gudr = gudbinding.GetUserDetailsOperation(gud);
        //tCities.Text = gudr.InnerXml;*/

        String UserDetailsResponse = Util.run(RequestXML.getUserDetailsRequest);
        
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(UserDetailsResponse);
        XmlElement getUserDetailsResponse = (XmlElement)xmldoc.SelectSingleNode("//*[local-name()='GetUserDetailsResponse']");
       
        Util.userDetails.authuserdn = getUserDetailsResponse.SelectSingleNode("//*[local-name()='authuserdn']").InnerText;
        Util.userDetails.description = getUserDetailsResponse.SelectSingleNode("//*[local-name()='description']").InnerText;

        XmlNodeList orglist = getUserDetailsResponse.SelectNodes("//*[local-name()='organization']");
        XmlDocument tmpdoc = new XmlDocument();
        foreach (XmlNode orgnode in orglist)
        {
            tmpdoc.LoadXml(orgnode.Clone().OuterXml);
            String dn = tmpdoc.SelectSingleNode("//*[local-name()='dn']").InnerText;
            String description = tmpdoc.SelectSingleNode("//*[local-name()='description']").InnerText; ;
            String organizationaluserdn = tmpdoc.SelectSingleNode("//*[local-name()='organizationaluser']//*[local-name()='dn']").InnerText;
            Boolean isdefault = false;
            if (tmpdoc.SelectSingleNode("//@default") != null)
            {
                isdefault = Boolean.Parse(tmpdoc.SelectSingleNode("//@default").InnerText);
            }
            Organization organization = new Organization(dn, description, organizationaluserdn,isdefault);
            Util.userDetails.organizationsList.Add(organization);
        }
    }

    protected void tCities_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("inbox.aspx?organization=" + RadioButtonList1.SelectedItem.Text + "&organizationdn=" + RadioButtonList1.SelectedItem.Value);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("createRequest.aspx?organization="+RadioButtonList1.SelectedItem.Text+"&organizationdn="+RadioButtonList1.SelectedItem.Value);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("notifications.aspx");
    }
}
